﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Service
{
    public partial class RequestAdd : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userid = Request["userid"] ?? "";
            string categoryid = Request["categoryid"] ?? "";
            string title = Request["title"] ?? "";
            string secretkey = Request["secret_key"] ?? "";

            if (!IsValidKey(secretkey, userid, categoryid, title))
            {
                Response.Write("112");//密钥不正确
                return;
            }

            if (Request["userid"] != null && Request["categoryid"] != null && Request["title"] != null)
            {
                Model.RequestInfo request = new Model.RequestInfo();
                //用户ID有效性检查
                if (BLL.UserInfoBLL.Instance.GetDetail(userid) != null)
                {
                    request.UserID = userid;
                }
                else
                {
                    Response.Write("301");//用户名无效
                    return;
                }

                //类别信息
                Model.ServiceCategory sc=BLL.ServiceCategoryBLL.Instance.GetDetail(categoryid);
                if (sc != null)
                {
                    request.CategoryName = sc.CategoryName;
                }
                else
                {
                    Response.Write("302");//类别信息无效
                    return;
                }
                //内容
                if (title.Length > 5 && title.Length < 140)
                {
                    request.Title = title;
                }
                else
                {
                    Response.Write("303");//标题长度必须在6～140之间
                    return;
                }

                //尚未用到描述信息的字段
                request.Description = "";
                request.RequestID = Guid.NewGuid().ToString();
                request.State = (int)Common.Enum.auvState.Enabled;
                request.IsDel = (int)Common.Enum.auvDel.NotDel;
                request.StatusID = (int)Common.Enum.RequestState.UnTreated;
                request.RequestFrom = "Mobile";

                BLL.RequestInfoBLL.Instance.Add(request);

                Response.Write("100");//添加成功！

            }
            else
            {
                Response.Write("111");//传入参数不正确
            }
        }

        private bool IsValidKey(string secretkey, string userid, string categoryid, string title)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + userid + categoryid + title + current_day).Substring(6, 22);
                    return secretkey == finalkey;

                }
            }
            else
            {
                return true;
            }
        }
    }
}